﻿using System;
using System.Windows;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using System.Linq;
using Wpf.CommonTools.StringExtension;
using System.Text.RegularExpressions;

namespace Convertor.Plugin.AlertBox
{
    public class Plugin : PluginBase, IPluginOutputHeler, IPluginLoadPluginItems
    {
        #region Static Fun

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.AlertBox;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.AlertBox;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_System") + " - " + UILanguage.FindResource("Plugin_AlertBox_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_AlertBox_Desc"); }
        }

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            string alertMessage;
            switch (config.AlertType)
            {
                default:
                case AlertTypes.OutputContent:
                    alertMessage = content.OutputContent.ToString();
                    break;
                case AlertTypes.InputContent:
                    alertMessage = content.LastInputContent.ToString();
                    break;
                case AlertTypes.CustomContent:
                    try
                    {
                        alertMessage = string.Format(config.CustomContent, content.LastInputContent.ToString(), content.OutputContent.ToString());
                    }
                    catch
                    {
                        alertMessage = config.CustomContent;
                    }
                    break;
            }

            if (Log != null) { Log(this, string.Format(config.ToolTip, alertMessage.ToLimitLength(100)), LogTypes.Normal); }

            MessageBoxResult result = MessageBox.Show(alertMessage, UILanguage.FindResource("Plugin_AlertBox_Name"), (MessageBoxButton)MessageBoxButtonTypes.OKCancel);
            content.StopConvert = result != MessageBoxResult.OK;

            if (Log != null) { Log(this, string.Format(UILanguage.TryFindResource("Plugin_AlertBox_Log"), result.ToString()), LogTypes.Normal | LogTypes.HidePluginName); }

            if (content.StopConvert)
            {
                if (Log != null) { Log(this, UILanguage.TryFindResource("Plugin_Common_Cancel"), LogTypes.Alert | LogTypes.HidePluginName); }
            }

            return content;
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

        #region IPluginOutputHeler 成员

        public IConvertHelper OutputFunc { get; set; }

        #endregion

        #region IPluginLoadPluginItems 成员

        public LoadPluginItemsDelegate LoadPluginItems { get; set; }

        #endregion
    }
}
